package com.jzg.sys.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzg.sys.api.base.BaseFileService;
import com.jzg.sys.dao.entity.BaseFile;
import com.jzg.sys.dao.entity.BaseFileDetail;
import com.jzg.sys.dao.mapper.BaseFileDetailMapper;
import com.jzg.sys.dao.mapper.BaseFileMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 文件信息 服务实现类
 * </p>
 *
 * @author lzp
 * @since 2019-02-22
 */
@Service
public class BaseFileManager extends ServiceImpl<BaseFileMapper, BaseFile> implements BaseFileService {
    @Resource
    private BaseFileMapper baseFileMapper;
    @Resource
    private BaseFileDetailMapper baseFileDetailMapper;


    @Override
    public BaseFile getBaseFileByCode(String useCode, String sysCode) {
        return baseFileMapper.selectOne(new QueryWrapper<BaseFile>()
                .lambda()
                .eq(BaseFile::getUseCode, useCode.toUpperCase())
                .eq(BaseFile::getSysCode, sysCode)
        );
    }

    public List<BaseFileDetail> findDetail(String fileId) {
        return baseFileDetailMapper.selectList(new QueryWrapper<BaseFileDetail>()
                .lambda()
                .eq(BaseFileDetail::getFileId, fileId)
                .eq(BaseFileDetail::getStatus, true)
                .orderByAsc(BaseFileDetail::getSortNo)
        );
    }

    public List<BaseFileDetail> findDetailByTableId(String useCode, String tableId, String sysCode) {
        BaseFile baseFile = getBaseFileByCode(useCode, sysCode);
        return baseFileDetailMapper.selectList(new QueryWrapper<BaseFileDetail>()
                .lambda()
                .eq(BaseFileDetail::getFileId, baseFile.getFileId())
                .eq(BaseFileDetail::getTableId, tableId)
                .eq(BaseFileDetail::getStatus, true)
                .orderByAsc(BaseFileDetail::getSortNo)
        );
    }

    public void saveFileDetail(BaseFileDetail baseFileDetail) {
        baseFileDetailMapper.delete(new UpdateWrapper<BaseFileDetail>()
                .lambda()
                .eq(StringUtils.isNotBlank(baseFileDetail.getTableId()),
                        BaseFileDetail::getTableId, baseFileDetail.getTableId())
                .eq(baseFileDetail.getSortNo() != null,
                        BaseFileDetail::getSortNo, baseFileDetail.getSortNo())
                .eq(BaseFileDetail::getStatus, true)
        );
        baseFileDetailMapper.insert(baseFileDetail);
    }

    @Override
    public int delByFileName(String fileName) {
        List<BaseFileDetail> fileDetails = baseFileDetailMapper.selectList(new QueryWrapper<BaseFileDetail>()
                .lambda()
                .eq(BaseFileDetail::getFileUrl, "upload/" + fileName));
        if (fileDetails.size() == 1) {
            return baseFileDetailMapper.deleteById(fileDetails.get(0).getFileDetailId());
        } else {
            return 0;
        }
    }

    @Override
    public void delImgBySortNo(String useCode, String tableId, Long sortNo, String sysCode) {
        BaseFile baseFile = getBaseFileByCode(useCode, sysCode);
        List<BaseFileDetail> fileDetails = baseFileDetailMapper.selectList(new QueryWrapper<BaseFileDetail>()
                .lambda()
                .eq(BaseFileDetail::getFileId, baseFile.getFileId())
                .eq(BaseFileDetail::getTableId, tableId)
                .eq(BaseFileDetail::getStatus, true)
                .eq(BaseFileDetail::getSortNo, sortNo)
        );
        if (fileDetails.size() == 1) {
            baseFileDetailMapper.deleteById(fileDetails.get(0).getFileDetailId());
        }
    }

    /**
     * @param useCode
     * @param tableId
     * @param sysCode
     * @return
     * @Title: getImgUrl
     * @Description: 获取单图
     * @author 李志鹏 lzp
     * @date 2016年8月25日 下午4:16:31
     */
    public String getImgUrl(String useCode, String tableId, String sysCode) {
        BaseFile baseFile = getBaseFileByCode(useCode, sysCode);
        if (baseFile == null) {
            return "";
        }
        BaseFileDetail baseFileDetail = baseFileDetailMapper.selectOne(new QueryWrapper<BaseFileDetail>()
                .lambda()
                .eq(BaseFileDetail::getFileId, baseFile.getFileId())
                .eq(StringUtils.isNotBlank(tableId), BaseFileDetail::getTableId, tableId)
                .eq(BaseFileDetail::getSysCode, sysCode)
                .eq(BaseFileDetail::getStatus, true)
                .orderByAsc(BaseFileDetail::getSortNo)
        );
        return baseFileDetail == null ? "" : baseFileDetail.getFileUrl();
    }

    /**
     * @param useCode
     * @param tableId
     * @param sysCode
     * @return
     * @Title: getFileName
     * @Description: 获取文件原名
     * @author 李志鹏 lzp
     * @date 2016年8月25日 下午4:16:31
     */
    public String getFileName(String useCode, String tableId, String sysCode) {
        BaseFile baseFile = getBaseFileByCode(useCode, sysCode);
        if (baseFile == null) {
            return "";
        }
        BaseFileDetail baseFileDetail = baseFileDetailMapper.selectOne(new QueryWrapper<BaseFileDetail>()
                .lambda()
                .eq(BaseFileDetail::getFileId, baseFile.getFileId())
                .eq(StringUtils.isNotBlank(tableId), BaseFileDetail::getTableId, tableId)
                .eq(BaseFileDetail::getSysCode, sysCode)
                .eq(BaseFileDetail::getStatus, true)
                .orderByAsc(BaseFileDetail::getSortNo)
        );
        return baseFileDetail == null ? "" : baseFileDetail.getFileName();
    }

    public String getFileUrl(String useCode, String tableId, String sysCode) {
        return getImgUrl(useCode, tableId, sysCode);
    }

    /**
     * @param useCode
     * @param tableId
     * @param sysCode
     * @return
     * @Title: getImgUrls
     * @Description: 返回多图
     * @author 李志鹏 lzp
     * @date 2016年9月4日 上午1:19:20
     */
    public List<String> getImgUrls(String useCode, String tableId, String sysCode) {
        BaseFile baseFile = getBaseFileByCode(useCode, sysCode);
        return baseFileDetailMapper.selectList(new QueryWrapper<BaseFileDetail>()
                .lambda()
                .eq(BaseFileDetail::getFileId, baseFile.getFileId())
                .eq(StringUtils.isNotBlank(tableId), BaseFileDetail::getTableId, tableId)
                .eq(BaseFileDetail::getSysCode, sysCode)
                .eq(BaseFileDetail::getStatus, true)
                .orderByAsc(BaseFileDetail::getSortNo)).
                stream()
                .map(BaseFileDetail::getFileUrl)
                .collect(Collectors.toList());
    }

    public Long getFileMaxSortNo(String useCode, String tableId, String sysCode) {
        BaseFile baseFile = getBaseFileByCode(useCode, sysCode);
        BaseFileDetail baseFileDetail = baseFileDetailMapper.selectOne(new QueryWrapper<BaseFileDetail>()
                .lambda()
                .eq(BaseFileDetail::getFileId, baseFile.getFileId())
                .eq(BaseFileDetail::getTableId, tableId)
                .eq(BaseFileDetail::getSysCode, sysCode)
                .eq(BaseFileDetail::getStatus, true)
                .orderByDesc(BaseFileDetail::getSortNo)
        );
        return baseFileDetail == null ? 0 : baseFileDetail.getSortNo();
    }
}
